Export Folding is not UTF-8 Safe
-
Fjan
Very clever. You only checked the remainder and it worried me that there could be a UTF-8 character that would split into something that was still valid, but I checked the UTF-8 spec and that doesn't seem possible. In fact, you can also determine if you are dealing with a split-in-the-middle character by simply checking if the top two bits are '10', which may be a bit cheaper than String.unpack
-
Rick DeNatale
- State changed from new to resolved
I suspect that String.unpack is actually quicker than checking the byte with ruby, since it's a C 'primitive'
In any case I doubt that it makes much performance difference, I trust String#pack to get the utf-8 rules right.
-
Fjan
By the way, here is what I came up with, slightly shorter but a bit less elegant:
class String
def ical_wrap if (l=self.length)>75 then x=75 begin if self[x]&192!=128 then self.insert(x,"\r\n ") # change for Ruby 1.9 elsif self[x-=1]&192!=128 then self.insert(x,"\r\n ") else self.insert(x-=1,"\r\n ") end x+=77;l+=3 end while x<l end self end end
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.